package com.miyake.client.datamanage;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.miyake.client.DataServiceAsync;

public class SecurityLevelWidget extends Composite {

	public SecurityLevelWidget(final NodeProperty node, final DataServiceAsync service) {
		HorizontalPanel panel = new HorizontalPanel();
		final ListBox listBox = new ListBox();
		this.initWidget(panel);
		listBox.addItem("No One");
		listBox.addItem("Friends");
		listBox.addItem("Everyone");
		panel.add(new HTML("<b>Publish to</b>"));
		panel.add(listBox);
		if (node.getSecurityLevel() == 0) {
			listBox.setSelectedIndex(0);
		}
		else {
			listBox.setSelectedIndex(1);
		}
		
		listBox.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				setSecurityLevel(node.getKey(), listBox.getSelectedIndex(), service);
			}
		});
	}
	protected void setSecurityLevel(String key, Integer level,
			DataServiceAsync service) {
		service.setSecurityLevel(key, level, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {

			}
		});
	}
}
